package com.buanalintas.sandals.pos.bonussales;


import com.buanalintas.bynga.Controller;
import com.buanalintas.bynga.Dictionary;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Utility;
import com.buanalintas.bynga.entities.MasterUnit;
import com.buanalintas.bynga.formatter.DateFormatter;
import com.buanalintas.bynga.formatter.MoneyFormatter;
import com.buanalintas.sandals.pos.entities.PosMasterSales;
import com.buanalintas.sandals.pos.entities.PosTransactionBonusSales;
import com.buanalintas.sandals.pos.entities.PosTransactionSale;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author dedy
 */
public class BonusSales extends Page {

    private Field bonusSalesId;
    private Field bonusSalesFromDate;
    private Field bonusSalesToDate;
    private Field bonusSalesTotalSale;
    private Field bonusSalesNominal;
    private Field bonusSalesSalesId;
    private Field bonusSalesUnitId;
    private Field bonusSalesTimestamp;

    public BonusSales() {
        super();
        setTheClass(PosTransactionBonusSales.class);

        bonusSalesId = new Field();
        bonusSalesId.setName("bonusSalesId");
        bonusSalesId.setCaption("id");
        bonusSalesId.setListProperties(" align=\"right\" width=\"5%\" nowrap");
        bonusSalesId.setInputProperties(" size=\"20\"");
        getFields().add(bonusSalesId);

        bonusSalesUnitId = new Field();
        bonusSalesUnitId.setName("bonusSalesUnitId");
        bonusSalesUnitId.setCaption("unit");
        bonusSalesUnitId.setListProperties(" width=\"20%\"");
        bonusSalesUnitId.setInputProperties(" size=\"15\" readonly");
        bonusSalesUnitId.setRequired(true);
        bonusSalesUnitId.getSelectorColumns();
        bonusSalesUnitId.getViewColumns().addAll(bonusSalesUnitId.getSelectorColumns());
        getFields().add(bonusSalesUnitId);
        
        bonusSalesSalesId = new Field();
        bonusSalesSalesId.setName("bonusSalesSalesId");
        bonusSalesSalesId.setCaption("sales");
        bonusSalesSalesId.setListProperties(" width=\"20%\"");
        bonusSalesSalesId.setInputProperties(" size=\"15\" readonly");
        bonusSalesSalesId.setRequired(true);
        bonusSalesSalesId.getSelectorColumns();
        bonusSalesSalesId.getViewColumns().addAll(bonusSalesSalesId.getSelectorColumns());
        getFields().add(bonusSalesSalesId);

        bonusSalesFromDate = new Field();
        bonusSalesFromDate.setName("bonusSalesFromDate");
        bonusSalesFromDate.setCaption("from date");
        bonusSalesFromDate.setListProperties(" align=\"center\" width=\"10%\" nowrap");
        bonusSalesFromDate.setInputProperties(" size=\"8\"");
        bonusSalesFromDate.setRequired(true);
        bonusSalesFromDate.setDefaultValue(Utility.getTimestamp());
        getFields().add(bonusSalesFromDate);

        bonusSalesToDate = new Field();
        bonusSalesToDate.setName("bonusSalesToDate");
        bonusSalesToDate.setCaption("to date");
        bonusSalesToDate.setListProperties(" align=\"center\" width=\"10%\" nowrap");
        bonusSalesToDate.setInputProperties(" size=\"8\"");
        bonusSalesToDate.setRequired(true);
        bonusSalesToDate.setDefaultValue(Utility.getTimestamp());
        getFields().add(bonusSalesToDate);
        
       

        bonusSalesTotalSale = new Field();
        bonusSalesTotalSale.setName("bonusSalesTotalSale");
        bonusSalesTotalSale.setCaption("total sale");
        bonusSalesTotalSale.setListProperties(" align=\"right\" width=\"20%\" nowrap");
        bonusSalesTotalSale.setInputProperties(" size=\"8\" readonly");
        bonusSalesTotalSale.setRequired(true);
        bonusSalesTotalSale.setFormater(MoneyFormatter.getInstance());
        getFields().add(bonusSalesTotalSale);
        
        bonusSalesNominal = new Field();
        bonusSalesNominal.setName("bonusSalesNominal");
        bonusSalesNominal.setCaption("Nominal");
        bonusSalesNominal.setListProperties(" align=\"right\" width=\"20%\" nowrap");
        bonusSalesNominal.setInputProperties(" size=\"8\"");
        bonusSalesNominal.setFormater(MoneyFormatter.getInstance());
        getFields().add(bonusSalesNominal);

        bonusSalesTimestamp = new Field();
        bonusSalesTimestamp.setName("bonusSalesTimestamp");
        bonusSalesTimestamp.setCaption("timestamp");
        bonusSalesTimestamp.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        bonusSalesTimestamp.setInputProperties(" size=\"8\" readonly");
        bonusSalesTimestamp.setDefaultValue(Utility.getTimestamp());
        getFields().add(bonusSalesTimestamp);


        fillFields();
        
        getFieldsAdd().remove(bonusSalesUnitId);
        getFieldsAdd().remove(bonusSalesId);
        getFieldsAdd().remove(bonusSalesTimestamp);
//        getFieldsAdd().remove(bonusSalesToDate);
        getFieldsAdd().remove(bonusSalesTotalSale);
//        getFieldsAdd().remove(bonusSalesNominal);
        
        getFieldsEdit().remove(bonusSalesId);
        
        getFieldsList().remove(bonusSalesId);
        
        getFieldsPrint().remove(bonusSalesId);
        getFieldsPrintList().remove(bonusSalesId);
        getFieldsView().remove(bonusSalesId);

        getFieldsSearch().remove(bonusSalesId);

        setController(new BonusSaleController(this));
        setExportList(getClass().getResourceAsStream("BonusSalesList.jasper"));
        setExport(getClass().getResourceAsStream("BonusSales.jasper"));
    }

    @Override
    public void beforeExport() {
        getExportParams().put("title", Dictionary.getValue(this, "Bonus Sales"));
    }

    @Override
    public void beforeExportMulti() {
        beforeExport();

    }

    @Override
    public void afterInit(HttpServletRequest req, HttpServletResponse res) {
        bonusSalesTimestamp.setFormater(DateFormatter.getInstanceDate(this));
//        bonusSalesSalesId.getSelectorColumns().add(new String[]{"salesName", " Name", "width=\"30%\" nowrap"});
//        bonusSalesSalesId.setSelectorCondition("t.salesUnitId.unitId="+getCurrentUnit().getUnitId());
//        getFields().add(bonusSalesSalesId);
    }

    class BonusSaleController extends Controller {

        public BonusSaleController(Page page) {
            super(page);
        }

         @Override
        public void beforeInsert(Object data) {
            PosTransactionBonusSales bonusSales = (PosTransactionBonusSales) data;
            EntityManager em = getEntityManager();

            MasterUnit unitId = getCurrentUnit();
            PosMasterSales salesId = bonusSales.getBonusSalesSalesId();
            Date start = bonusSales.getBonusSalesFromDate();
            Date end = bonusSales.getBonusSalesToDate();
//            System.out.println("======================>>>" + start + "====" + end );
            Query query = em.createQuery("SELECT t FROM PosTransactionSale t WHERE (t.saleDate BETWEEN :start AND :end)  AND (t.saleSalesId =:sales) AND (t.saleUnitId =:unit)");
//            Query query = em.createQuery("SELECT t FROM PosTransactionSale t WHERE t.saleUnitId =:unit");

            query.setParameter("sales", salesId);
            query.setParameter("unit", unitId);
            query.setParameter("start", start);
            query.setParameter("end", end);

            BigDecimal saleTotal = new BigDecimal(0);
            
//            //SUM TOTAL
//            Query querySum = em.createQuery("SELECT SUM(t.saleTotal) FROM PosTransactionSale t WHERE (t.saleDate BETWEEN :start AND :end) AND (t.saleUnitId =:unit)");
//            List<PosTransactionSale> hasil = new ArrayList<PosTransactionSale>();
//            hasil = querySum.getResultList();
//            saleTotal = hasil.get(0).get
//            saleTotal = querySum.getParameter();
            try {
                List<PosTransactionSale> sale = query.getResultList();
                for (int i = 0; i < sale.size(); i++) {
                    System.out.println("======================>>>" + sale.get(i).getSaleTotal());

                    saleTotal = saleTotal.add(sale.get(i).getSaleTotal());
                }
            } catch (Exception e) {
            }
            bonusSales.setBonusSalesUnitId(unitId);
            bonusSales.setBonusSalesTotalSale(saleTotal);
            bonusSales.setBonusSalesTimestamp(Utility.getTimestamp());

        }

        @Override
        public void afterInsert(Object data) {
            super.afterInsert(data);
        }
    }
}
